package com.example.pclimb.dao;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import com.example.pclimb.model.ClimbRequest;

import java.util.Date;
import java.util.List;

/**
 * 陪爬申请数据访问接口
 */
@Dao
public interface ClimbRequestDao {
    @Insert
    long insert(ClimbRequest climbRequest);

    @Update
    void update(ClimbRequest climbRequest);

    @Delete
    void delete(ClimbRequest climbRequest);

    @Query("SELECT * FROM climb_requests WHERE id = :id")
    LiveData<ClimbRequest> getClimbRequestById(int id);

    @Query("SELECT * FROM climb_requests WHERE userId = :userId")
    LiveData<List<ClimbRequest>> getClimbRequestsByUserId(int userId);

    @Query("SELECT * FROM climb_requests WHERE guideId = :guideId")
    LiveData<List<ClimbRequest>> getClimbRequestsByGuideId(int guideId);

    @Query("SELECT * FROM climb_requests WHERE status = 0")
    LiveData<List<ClimbRequest>> getPendingClimbRequests();

    @Query("SELECT * FROM climb_requests WHERE status = 1")
    LiveData<List<ClimbRequest>> getMatchedClimbRequests();

    @Query("SELECT * FROM climb_requests WHERE status = 2")
    LiveData<List<ClimbRequest>> getAwaitingPaymentClimbRequests();

    @Query("SELECT * FROM climb_requests WHERE status = 3")
    LiveData<List<ClimbRequest>> getCompletedClimbRequests();

    @Query("SELECT * FROM climb_requests WHERE status = 4")
    LiveData<List<ClimbRequest>> getCancelledClimbRequests();

    @Query("UPDATE climb_requests SET status = 1, guideId = :guideId WHERE id = :requestId")
    void matchRequest(int requestId, int guideId);

    @Query("UPDATE climb_requests SET status = 2 WHERE id = :requestId")
    void setAwaitingPayment(int requestId);

    @Query("UPDATE climb_requests SET status = 3 WHERE id = :requestId")
    void completeRequest(int requestId);

    @Query("UPDATE climb_requests SET status = 4 WHERE id = :requestId")
    void cancelRequest(int requestId);

    @Query("UPDATE climb_requests SET price = :price WHERE id = :requestId")
    void setPrice(int requestId, double price);

    @Query("UPDATE climb_requests SET isPaid = 1, status = 3 WHERE id = :requestId")
    void markAsPaid(int requestId);

    @Query("SELECT * FROM climb_requests WHERE climbDate BETWEEN :startDate AND :endDate")
    LiveData<List<ClimbRequest>> getClimbRequestsByDateRange(Date startDate, Date endDate);

    @Query("SELECT * FROM climb_requests WHERE location LIKE '%' || :location || '%'")
    LiveData<List<ClimbRequest>> getClimbRequestsByLocation(String location);
} 